import { createStore } from 'vuex'
import http from '../api/index'
import createPersistedState from 'vuex-persistedstate'

export default createStore({
  state: {
    windowWidth: 0,
    scrollTop: 0,
    backTop: {
      type: Function,
      defalut() {
        return null;
      }
    },
    isLogin: null
  },
  getters: {
    getWindowWidth(state) {
      return state.windowWidth
    },
    getScrollTop(state) {
      return state.scrollTop
    },
    getBackTop(state) {
      return state.backTop
    },
    getIsLogin(state) {
      return state.isLogin
    }
  },
  mutations: {
    setWindowWidth(state, value) {
      state.windowWidth = value
    },
    setScrollTop(state, value) {
      state.scrollTop = value
    },
    setBackTop(state, value) {
      state.backTop = value
    },
    setIsLogin(state, value) {
      state.isLogin = value
    }
  },
  actions: {
    async getUser(context) {
      try {
        let res = await http().getUserData();
        context.commit('setIsLogin',
          res.data.result[0],
        )
        console.log("获取数据");
      }catch(e) {
        context.commit('setIsLogin',
          null
        )
        console.error(e);
      }
    }
  },
  modules: {
  },
  plugins: [
    createPersistedState({
      storage: window.sessionStorage
    })
  ]
})
